define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'department/user/index' + location.search,
                    add_url: 'department/user/add',
                    edit_url: 'department/user/edit',
                    // del_url: 'department/user/del',
                    multi_url: 'department/user/multi',
                    jixiao_url: 'department/user/jixiao',
                    import_url: 'department/user/import',
                    table: 'department_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'department_id', title: __('Department_id')},
                        {field: 'lists.name', title: __('Lists.name'), operate: 'LIKE'},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {
                            field: 'avatar',
                            title: __('Avatar'),
                            operate: 'LIKE',
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'type',
                            title: __('身份'),
                            searchList: {"1": __('普通员工'), "2": __('代理商')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'is_leader',
                            title: __('Is_leader'),
                            searchList: {"0": __('否'), "1": __('是')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'is_manager',
                            title: __('Is_manager'),
                            searchList: {"0": __('否'), "1": __('是')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'is_super',
                            title: __('是否董事长'),
                            searchList: {"0": __('否'), "1": __('是')},
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'is_sign',
                            title: __('是否需要签到'),
                            searchList: {"0": __('否'), "1": __('是')},
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'is_leave',
                            title: __('是否离职'),
                            searchList: {"0": __('否'), "1": __('是')},
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'is_year_bonus',
                            title: __('是否参与激励计划收益'),
                            searchList: {"0": __('否'), "1": __('是')},
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'is_audit',
                            title: __('是否审核员'),
                            searchList: {"0": __('否'), "1": __('是')},
                            formatter: Table.api.formatter.toggle
                        },
                        {field: 'has_year_bonus', title: __('激励计划收益'), operate: false},
                        {field: 'jixiao', title: __('绩效'), operate: false},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"normal": __('Normal'), "hidden": __('Hidden')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'salary', //唯一标识、权限标识
                                    text: '查看月薪', //按钮显示的文字，支持function
                                    title: '查看月薪', //按钮显示的文字，支持function
                                    classname: 'btn btn-info btn-xs btn-dialog', //按钮的class，支持btn-dialog/btn-ajax/btn-addtabs
                                    url: 'month/Salary/index/user_id/{id}', //按钮的链接，支持使用{字段名}来占位替换，支持function
                                    disable: function (row) {
                                        if (row.type == 1) {
                                            return false;
                                        } else {
                                            return true;
                                        }
                                    },
                                    extend: '', //扩展的扩展属性，FastAdmin1.4.0+支持function
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        jixiao: function () {
            //只需要添加这行代码，就可调用上传组件
            require(['upload'], function (Upload) {
                Upload.api.plupload();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
